#include <stdexcept>
#include <cassert>
#include "timer.h"


	LARGE_INTEGER getFreq()
	{
		LARGE_INTEGER freq;
		BOOL check = QueryPerformanceFrequency(&freq);
		if(0 == freq.QuadPart || 0 == check)
		{
			throw std::out_of_range("freq error");
		}

		return freq;
	}

	Timer::Timer(): checkStart(false), freq(getFreq())
	{}

	void Timer::start()
	{
		BOOL check = QueryPerformanceCounter(&startTime);
		if(0 == startTime.QuadPart || 0 == check)
		{
			throw std::out_of_range("counter error");
		}
		checkStart = true;
	}

	double Timer::stop()
	{
		assert(true == checkStart);
		BOOL check = QueryPerformanceCounter(&stopTime);

		if(0 == stopTime.QuadPart || 0 == check)
		{
			throw std::out_of_range("counter error");
		}

		checkStart = false;

		return static_cast<double>((stopTime.QuadPart - startTime.QuadPart))/freq.QuadPart;
	}